package com.kl.modular.common.controller;


import com.alibaba.fastjson.JSON;
import com.kl.common.encrypt.MD5;
import com.kl.common.util.DateUtil;
import com.kl.common.util.HttpRequestUtil;
import com.kl.component.shiro.AuthorityUser;
import com.kl.modular.base.service.util.Underline2CamelUtil;
import com.kl.modular.system.dao.model.SystemLog;
import com.kl.modular.system.service.api.ISystemLogService;
import com.kl.modular.system.service.constant.SystemConStant;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * 公用controller
 *
 * controller类需继承
 *
 * @author xr_zjb
 * @date 2017/06/07 10:27
 *
 *
 */
public abstract class BaseController {

    @Autowired
    private ISystemLogService systemLogService;

    /**
     * 新增日志
     * @param request 请求
     * @param name 名称
     * @param logTypeCode
     * @param logDataCode
     * @param newObj
     */
    public void addLoging(HttpServletRequest request,
                          String name,
                          String logTypeCode,
                          String logDataCode,
                          Object newObj){

    }


    /**
     * shiro代理session
     * @return
     */
    public Session getShiroSession(){
        Session session = SecurityUtils.getSubject().getSession();
        return session;
    }

    /**
     * 清除用户SESSION
     */
    public void removeShiroSession(){
        if (SecurityUtils.getSubject().getSession() != null) {
            SecurityUtils.getSubject().logout();
        }
        //SecurityUtils.getSubject().getSession().removeAttribute(SystemConStant.SESSION_USER_KEY);
    }

    /**
     * 会话用户对象
     * @return
     */
    public AuthorityUser getAuthorithUser(){
        Session session = getShiroSession();
        AuthorityUser authUser = (AuthorityUser) session.getAttribute((SystemConStant.SESSION_USER_KEY));
        return authUser;
    }

    /**
     * 会话用户
     * @return
     */
    public String getAuthUserAdminId(){
        AuthorityUser authUser = getAuthorithUser();
        if(authUser == null){
            return null;
        }
        return authUser.getAdminId();
    }

    /**
     * 会话用户
     * @return
     */
    public Long getAuthUserId(){
        AuthorityUser authUser = getAuthorithUser();
        if(authUser == null){
            return null;
        }
        return authUser.getId();
    }

    /**
     * 用户类型
     * @return
     */
    public String getAuthResourceType(){
        AuthorityUser authUser = getAuthorithUser();
        if(authUser == null){
            return null;
        }
        return authUser.getUserType();
    }

    /**
     * 获取session区域ID
     * @return
     */
    public Long getAreaId(){
        AuthorityUser authUser = getAuthorithUser();
        if(authUser == null){
            return null;
        }
        return authUser.getAreaId();
    }


    /**
     * 获取项目绝对路径
     * @param request
     * @return
     */
    public String getCtx(HttpServletRequest request) {
        String scheme = request.getScheme();
        String serverName = request.getServerName();
        int port = request.getServerPort();
        String path = request.getContextPath();
        String basePath = scheme + "://" + serverName;
        if(port != 80){
            basePath = basePath + ":" + port;
        }
        if(!org.apache.commons.lang.StringUtils.isEmpty(path)){basePath = basePath + "/" + path;

        }
        return basePath;
    }


}